# coding: utf-8
import os, sys
import time
import pickle, uuid, copy
from PyQt4 import QtCore

class Storage:
    def __init__(self, path):
        self.path = path
        # item: title, date, time, isloop, message
        self.data = []
        self.maxid = 0
        
        self.locker = QtCore.QMutex()
        self.load()

    def add(self, title, date, atime, isloop, message):
        self.maxid += 1
        id = self.maxid
        x = {'id':id, 'title':title, 'date':date, 'time':time, 
             'loop':isloop, 'msg':message, 
             'lasttime':0, 'ctime':int(time.time()),
             'enable':True}
        self.locker.lock()
        self.data.append(x)
        self.locker.unlock()
        return id

    def addrow(self, row):
        self.maxid += 1
        id = self.maxid
        row['id'] = id
        row['ctime'] = int(time.time())
        row['lasttime'] = 0
        row['enable'] = True

        self.locker.lock()
        self.data.append(row)
        self.locker.unlock()

        return id

    def updaterow(self, id, data):
        x = None
        self.locker.lock()
        for row in self.data:
            if row['id'] == id:
                x = row
                break
        if x:
            x['ctime'] = int(time.time())
            x['lasttime'] = 0
            x['enable'] = True
            
            x['title'] = data['title']
            x['date'] = data['date']
            x['time'] = data['time']
            x['loop'] = data['loop']
            x['msg']  = data['msg']

        self.locker.unlock()
        return id


    def remove(self, id):
        foundid = -1
        self.locker.lock()
        for i in range(0, len(self.data)):
            row = self.data[i]
            if row['id'] == id:
                foundid = i
                break

        if foundid >= 0:
            del self.data[foundid]
        self.locker.unlock()

    def lasttime_refresh(self, id):
        self.locker.lock()
        for row in self.data:
            if row['id'] == id:
                row['lasttime'] = int(time.time())
                break
        self.dump()
        self.locker.unlock()

    def find(self, id):
        x = None
        self.locker.lock()
        for row in self.data:
            if row['id'] == id:
                x = row
                break
        self.locker.unlock()
        return x

    def copy(self):
        self.locker.lock()
        x = copy.deepcopy(self.data)
        self.locker.unlock()

        return x

    def load(self):
        if os.path.isfile(self.path):
            f = open(self.path, 'r')
            x = pickle.load(f)
            f.close()
            
            self.locker.lock()
            self.data = x
            self.locker.unlock()
            self.maxid = max([r['id'] for r in x])

    def dump(self):
        f = open(self.path, 'w')
        pickle.dump(self.data, f)
        f.close()


